<?php

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

/**
 * View class for the Settings screen
 *
 * @package Joomla
 * @subpackage EventList
 * @since 0.9
 */
class TaxibookingViewSettings extends JView {

    function display($tpl = null) {

        $app = JFactory::getApplication();

        //initialise variables
        $document = & JFactory::getDocument();
        $acl = & JFactory::getACL();
        $uri = & JFactory::getURI();
        $user = & JFactory::getUser();
        $editor = & JFactory::getEditor();

        //get data from model
        $model = & $this->getModel();
        $elsettings = & $this->get('Data');
        $groups = $user->get('groups');
        $gid = array_keys($groups);
        //only admins have access to this view
        if ($gid[0] != 8 && $gid[0] != 7) {
            JError::raiseWarning('SOME_ERROR_CODE', JText::_('ALERTNOTAUTH'));
            $app->redirect('index.php?option=com_taxibooking');
        }

        // fail if checked out not by 'me'
        if ($model->isCheckedOut($user->get('id'))) {
            JError::raiseWarning('SOME_ERROR_CODE', JText::_('EDITED BY ANOTHER ADMIN'));
            $app->redirect('index.php?option=com_taxibooking');
        }

        // Get the page/component configuration
        $params = $app->getParams('com_taxibooking');

        //add css file
        $document->addStyleSheet($this->baseurl . '/components/com_taxibooking/assets/css/booking.css');

        // right from the menu item itself
        if (is_object($menu)) {
            $menu_params = new JParameter($menu->params);

            if (!$menu_params->get('page_title')) {
                $params->set('page_title', $menu->name);
            }
        } else {
            $params->set('page_title', JText::_('Taxi Booking'));
        }

        //Get the lists
        $lists = $this->_buildEditLists($elsettings);

        //assign data to template
        $this->assignRef('elsettings', $elsettings);
        $this->assignRef('lists', $lists);
        $this->assignRef('request_url', $uri->toString());
        $this->assignRef('editor', $editor);

        parent::display($tpl);
    }

    /**
     * Creates the item submit form
     *
     * @since 1.0
     */
    function _buildEditLists($elsettings) {
        $db = & JFactory::getDBO();

        $query = 'SELECT `currency_id`, `currency_code`, `currency_name`'
                . ' FROM #__taxibooking_currency'
                . ' ORDER BY `currency_name` ASC'
        ;
        $db->setQuery($query);
        $db->query();
        $rows = $db->loadObjectList();

        $currency_options = array();
        $currency_options[] = JHTML::_('select.option', '', JText::_('SELECT CURRENCY'));
        if (!empty($rows)) {
            foreach ($rows as $row) {
                $currency_options[] = JHTML::_('select.option', $row->currency_code, $row->currency_name);
            }
        }
        $lists['currency'] = JHTML::_('select.genericlist', $currency_options, 'currency', '', 'value', 'text', $elsettings->currency);

        return $lists;
    }

    function getToolbar() {
        // add required stylesheets from admin template
        $document = & JFactory::getDocument();
        $document->addStyleSheet('administrator/templates/system/css/system.css');
        //now we add the necessary stylesheets from the administrator template
        //in this case i make reference to the bluestork default administrator template in joomla 1.6
        $document->addCustomTag(
                '<link href="administrator/templates/bluestork/css/template.css" rel="stylesheet" type="text/css" />' . "\n\n" .
                '<!--[if IE 7]>' . "\n" .
                '<link href="administrator/templates/bluestork/css/ie7.css" rel="stylesheet" type="text/css" />' . "\n" .
                '<![endif]-->' . "\n" .
                '<!--[if gte IE 8]>' . "\n\n" .
                '<link href="administrator/templates/bluestork/css/ie8.css" rel="stylesheet" type="text/css" />' . "\n" .
                '<![endif]-->' . "\n" .
                '<link rel="stylesheet" href="administrator/templates/bluestork/css/rounded.css" type="text/css" />' . "\n"
        );
        //load the JToolBar library and create a toolbar
        jimport('joomla.html.toolbar');
        $bar = & new JToolBar('toolbar');
        //and make whatever calls you require
        $bar->appendButton('Standard', 'save', 'Save', 'save', false);
//        $bar->appendButton('Separator');
//        $bar->appendButton('Standard', 'cancel', 'Cancel', 'cancel', false);
        //generate the html and return
        return $bar->render();
    }

}